package cn.zyp.stusystem.controller;


import cn.zyp.stusystem.entity.GradeClass;
import cn.zyp.stusystem.service.ClassService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/class")
public class ClassController {

    //测试git

    @Autowired
    private ClassService classService;

    //根据年级查询班级列表
    @GetMapping("/list")
    public Map<String, Object> getClassList(@RequestParam Integer grade,@RequestParam(required = false) String name){
        List<GradeClass> gradeClasses = classService.findClassByGradeId(grade);
        System.out.println(gradeClasses);

        //过滤班级名称
        if(name != null){
           gradeClasses= gradeClasses.stream().filter(gradeClass -> gradeClass.getName().contains(name)).toList();
        }
        Map<String,Object> result = new HashMap<>();
        result.put("data",gradeClasses);
        return result;
    }

    //新增班级
    @PostMapping("/add")
    public boolean addClass(@RequestBody GradeClass gradeClass){
        return classService.save(gradeClass);
    }

    //编辑班级
    @PutMapping("/edit")
    public boolean editClass(@RequestBody GradeClass gradeClass){
        return classService.updateById(gradeClass);
    }

    //删除班级
    @DeleteMapping("/delete/{id}")
    public boolean deleteClass(@PathVariable Integer id){
        if(classService.isClassHasStudent(id)){
            return false;
        }else {
            return classService.removeById(id);
        }
    }

    @GetMapping("/check")
    //检查班级是否有学生
    public Map<String,Object> checkClassHasStudent(@RequestParam Integer classId){
        Map<String,Object> result = new HashMap<>();
        result.put("data",classService.isClassHasStudent(classId));
        return result;
    }
}
